#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QListWidgetItem>
#include <QLabel>
#include <QByteArray>
#include <QTextCodec>
#include <QShortcut>
#include <QTimer>
#include <QPushButton>
#include <QMap>
#include <QMutex>
#include <QTime>

#include "Define.h"
#include "FileKey.h"

#ifdef Q_WS_WIN
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#endif

class UDPSender;
class TextEditor;
class FileSelect;
class MyProgressBar;
class FileManager;
class MessageManager;
class LogManager;
class Setting;
class IPName;
class GroupList;
namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

#ifdef Q_WS_WIN
	protected:
		bool winEvent(MSG *message, long *result);
	private slots:
		void TrayActivated(QSystemTrayIcon::ActivationReason reason);
	private:
		QSystemTrayIcon *_trayIcon;
		QMenu *_trayIconMenu;
		QAction *_quitAction;
		bool _hookInstalled;
#endif

protected:
	void closeEvent(QCloseEvent *event);
	void contextMenuEvent(QContextMenuEvent *event);
	void dragEnterEvent(QDragEnterEvent *event);
	void dropEvent(QDropEvent *event);

private:
	bool InitStatusBar();
	bool InitTimer();
	void InitWidget();
	void InitActionShortcut();
	bool DeleteItemInList(QString str);
	bool IsAlreadyExistInSendList(QString groupName);
	void RefreshWidget();
	void UpdateConnectList(QString group, QString name, QString currentGroupText, int gCnt, bool add);
	bool RemoveFileList(FileKey key);
	void ManageLogWidget(bool isTx, int curIdx, int sum, bool putMsg = true);
	void PutMessage(QByteArray stream, bool log, bool isTx);

private slots:
	void on_button_moveWithList_clicked();
 void on_button_loadGroup_clicked();
 void on_button_saveGroup_clicked();
 void on_button_rDeleteAll_clicked();
	void on_button_sDeleteAll_clicked();
	void on_button_rFix_clicked();
	void on_button_rDelFlag_clicked();
	void on_button_ipName_clicked();
	void on_list_sendFile_itemDoubleClicked(QListWidgetItem* item);
	void on_button_refresh_clicked();
	void on_button_fileSend_clicked();
	void on_button_sDelete_clicked();
	void on_button_sLatest_clicked();
	void on_button_sFirst_clicked();
	void on_button_sNext_clicked();
	void on_button_sPrevious_clicked();
	void on_button_rLatest_clicked();
	void on_button_rFirst_clicked();
	void on_button_rNext_clicked();
	void on_button_rPrevious_clicked();
	void on_button_rDelete_clicked();
	void on_button_reply_clicked();
	void on_button_sSend_clicked();
	void on_button_insert_clicked();
	void on_button_insertAll_clicked();
	void on_button_remove_clicked();
	void on_button_removeAll_clicked();
	void on_pushButton_clicked();
	void OnProgressButtonClicked();
	void ExitMenuClicked();
	void SettingMenuClicked();
	void on_comboBox_activated(QString str);
	void ProcessUDPPacket(UDPPacket *packet);
	void ProcessTCPPacket(QByteArray stream);
	void OnError(QString errorString);
	void OnFileSelected(QString filepath, QString fileName);
	void CreateProgressBar(FileKey key, long long size);
	void UpdateProgressBar(FileKey key, int bytes);
	void RemoveProgressBar(FileKey key);
	void EnableProgressBarButton(FileKey key);
	void AddSendListFromIPName(QString str);
	void OnNetworkConfigChange(int prePort, QString preName);
	void OnRemoveAllLog();
	void OnOpenDirectory();
	void OnSaveWindowSize();
	void OnCustomGroupSelected(QString gName);
	void OnEverySecond();

signals:
	void sigAddSendTask(QHostAddress addr, QString filePath);
	void sigBroadcastIPChange(bool clicked);

private:
    Ui::MainWindow *ui;
	QLabel *_labelClientCount;
	QLabel *_labelMyInfo;
	QLabel *_labelCurrentTime;
	QLabel *_labelCurrentStatus;
	QPushButton _progressButton;
	QTimer *_timer;
	QTextCodec *_dec;
	QTextCodec *_enc;
	QShortcut *_shortcutSend;
	QShortcut *_shortcutReply;
	QShortcut *_shortcutOpenDownLoadFolder;
	QShortcut *_shortcutOpenLogFolder;
	QAction *_actionOpenDownloadFolder;
	QAction *_actionOpenLogFolder;
	QAction *_actionSaveWindowSize;
	QString _fileName;
	QString _filePath;
	bool _isfileSend;
	bool _exit;
	bool _fixedView;
	QMap<FileKey, MyProgressBar*> _barMap;
	QMutex _mutex;
	int _newMsgCnt;
	QTime _lastMsgReceiveTime;

	UDPSender *_udpSender;
	TextEditor *_textEditor;
	FileSelect *_fileSelect;
	FileManager *_fileManager;
	MessageManager *_msgManager;
	Setting *_setting;
	IPName *_ipName;
	LogManager *_logManager;
	GroupList *_groupList;

};

#endif // MAINWINDOW_H
